<?php
namespace app\api\controller;

use app\BaseController;
use app\model\Coupon;
use app\model\MemberCoupon as SelfModel;
use app\model\Member;

use app\model\MemberCouponInvite;
use think\facade\Cookie;
use think\Response;
use think\Request;
use think\facade\Db;
class MemberCoupon extends Base
{


    public function initialize()
    {
        parent::initialize();
        $this->model = "\app\model\MemberCoupon";
        $this->list_rows = 10;
        $this->index = ['id','member_id','member_mobile','title','content','big_price','small_price','status','start_time','end_time','create_time'];
        $this->all = ['id','member_id','member_mobile','title','content','big_price','small_price','status','start_time','end_time','create_time'];
        $this->my = ['id','member_id','member_mobile','title','content','big_price','small_price','status','start_time','end_time','create_time'];
        $this->view = ['id','member_id','member_mobile','title','content','big_price','small_price','status','start_time','end_time','create_time'];
        $this->msg = '会员优惠券';
        
        $this->status = [['status','=','1']];
    }

    public function index(Request $request)
    {
        $data = SelfModel::withSearch(array_keys($request->param), $request->param)
        ->where([['status', '=', 1]])
        ->order(['sort' => 'asc', 'id' => 'desc'])
        ->paginate(20);
        return json(['code' => 200, 'message' => '列表', 'data' => $data]);
    }

    public function all(Request $request)
    {
        $data = SelfModel::withSearch(array_keys($request->param), $request->param)

        ->order(['id' => 'desc'])
        ->select();
        foreach ($data as &$v){
            $v['usenum'] = 3;
            $v['type'] = $v['status'];
            $v['nownum'] =(new MemberCouponInvite())->where('coupon_id',$v['id'])->count();
            if($v['status'] == 3){
                $v['term'] = '*该券仅使用一次';
            }else{
                $v['term'] = $v['day'].'天';

            }

            $v['price'] = $v['small_price'];
            $v['tip'] = '满'.$v['big_price'].'可用';
            $v['lapsetime'] = $v['end_time'];
        }
        return json(['code' => 200, 'message' => '全部(无分页)', 'data' => $data,'sql'=>Db::getlastsql()]);
    }


    public function create(Request $request)
    {

        $member_coupon = \app\model\MemberCoupon::where('member_id',$request->member_id)->where('coupon_id',$request->id)->find();
        if(!empty($member_coupon)){
            return json(['code' => 400, 'message' => '当前优惠券已经领取过了']);
        }
        $coupon = Coupon::where('id', $request->id)->find();
        $ret = SelfModel::create([
            'member_id' => $request->member_id,
            'coupon_id'=>$coupon['id'],
            'title' => $coupon['title'],
            'content' => $coupon['content'],
            'big_price' => $coupon['big_price'],
            'small_price' => $coupon['small_price'],
            'day'=>$coupon['day'],
            'status' => 3,
            'start_time' => date('Y-m-d H:i:s', time()),
            'end_time' => date('Y-m-d H:i:s', time() + $coupon['day'] * 3600 * 24),
        ]);
        if ($ret) {
            return json(['code' => 200, 'message' => '优惠券领取成功,请分享激活']);
        }
        return json(['code' => 400, 'message' => '详情']);
    }

    public function update(Request $request)
    {
        $info = SelfModel::find($request->id);
        $ret = $info->save($this->param);
        if ($ret !== false) {
        return json(['code' => 200, 'message' => '修改成功']);
        }
        return json(['code' => 200, 'message' => '失败']);
    }


    public function destory(Request $request)
    {
        $ret = SelfModel::where('id','in',$request->id)->update(['delete_time' => date('Y-m-d H:i:s')]);
        if ($ret) return json(['code' => 200, 'message' => '删除成功']);
        return json(['code' => 400, 'message' => '失败']);
    }

    public function view(Request $request)
    {
        $data = SelfModel::find($request->id);
        $data['content'] = str_replace('<img','<img style="width: 100%;!important;display:block;max-width:100%;text-align:center"',$data['content']);
        return json(['code' => 200, 'message' => '详情', 'data' => $data]);
    }

    public function html(Request $request)
    {
        $data = SelfModel::where('id', $request->id)->value($request->field);
        $content = '<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">';
        $content .= '<style> img {vertical-align:middle; width:auto; max-width:100%;} </style>';
        $content .= $data;
        return Response::create($content, 'html');
    }

    public function json(Request $request)
    {
        $data = SelfModel::where('id', $request->id)->value($request->field);
        $data = str_replace('src="', 'src="' . $request->domain(), $data);
        return json($data);
    }

    public function my(Request $request)
    {
        $data = SelfModel::where('member_id', $request->member_id)->paginate(20);
        return json(['code' => 200, 'message' => '发布列表', 'data' => $data]);
    }




}
